package com.app.controller;

import com.app.common.*;
import com.app.model.*;
import com.app.service.*;
import com.app.util.*;
import org.thymeleaf.TemplateEngine;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author: Rujh
 * @Date: 2022/10/29 15:26
 */
@RestController
@RequestMapping("/")
public class AttentionController {

    @RequestMapping(value = "/getResumePDF", method = RequestMethod.GET)
    public Result<Object> getAttentionList(HttpServletRequest request) throws Exception {
        String token = request.getHeader("token");
        String mobile = request.getParameter("mobile");
        if(StringTool.isEmpty(mobile))
        {
            mobile = TokenUtils.nothingness(token);
        }
        if (mobile == null) {
            return Result.error(405, "token不存在");
        }
        UserModel user = userService.getUserByMobile(mobile);
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        return Result.success(test(user));
    }

    @Autowired
    private TemplateEngine engine;

    @Autowired
    UserService userService;

    @Autowired
    CustomerService customerService;
    @Autowired
    CusWorkExperienceService cusWorkExperienceService;
    @Autowired
    JobIntentionService jobIntentionService;
    @Autowired
    PositionService positionService;

    @Autowired
    private JobAdditionalService jobAdditionalService;

    @Autowired
    CusItemExpService cusItemExpService;

    @Autowired
    CusEducationService cusEducationService;

    @Autowired
    public CusResumeLangskillService cusResumeLangskillService;

    @Autowired
    LanguageService languageService;

    public String test(UserModel user) throws Exception {
        // 创建model，需要指定模版引擎和具体的模版，“报告模版”指的是资源目录下/html2pdfTemplate/报告模版.html文件。如果是springboot项目，那么就是在resources文件夹下面


        CustomerModel cus = customerService.getCustomerById(user.getCId());
        Model model = new Model(engine, "test");

        model.setMobile(user.getMobile());
        model.setWeChat(cus.getWeChat());
        List<CusWorkExperienceModel> workExpList = cusWorkExperienceService.getWorkExpList(cus.getId());
        if (workExpList != null && workExpList.size() > 0)//工作经验
        {
            Timestamp time = new Timestamp(new SimpleDateFormat("yyyy-MM-dd").parse(workExpList.get(0).getWorkTimeStart() + "-01").getTime());
            String date = DateUtil.yearCompare(time, CommonHelp.getNowTimestamp());
            model.setWorkingHours(date + "年");
        } else {
            model.setWorkingHours("无工作经验");
        }

        List<JobIntentionModel> jobIntentionModelList = jobIntentionService.getIntentionListByCustomerId(cus.getId());
        if (jobIntentionModelList != null) {
            JobIntentionModel jobIntentionModel = jobIntentionModelList.get(0);
            PositionModel positionModel = positionService.getModelById(jobIntentionModel.getPost());
            model.setIntention(positionModel.getJobTitle());
            for (int i = 0; i < AreasTool.AREA_VO_LIST.size(); i++) {
                if (Integer.valueOf(jobIntentionModel.getRegArea()) == AreasTool.AREA_VO_LIST.get(i).getId()) {
                    model.setPlace(AreasTool.AREA_VO_LIST.get(i).getData());
                }
            }
            model.setWages(jobIntentionModel.getSalaryMin() + "-" + jobIntentionModel.getSalaryMax() + "元");
        } else {
            model.setIntention("暂无意向");
            model.setPlace("暂无");
            model.setWages("暂无");
        }
        model.setRealName(cus.getRealName());
        String sex = "";
        if (cus.getSex() == 1) {
            sex = "男";
        } else {
            sex = "女";
        }
        model.setSex(sex);
        if (cus.getBirthday() != null)//年龄
        {
            Timestamp time = new Timestamp(new SimpleDateFormat("yyyy-MM-dd").parse(cus.getBirthday() + "-01").getTime());
            String date = DateUtil.yearCompare(time, CommonHelp.getNowTimestamp());
            model.setAge(date + "岁");
        } else {
            model.setAge("暂无年龄");
        }
        if (user.getFaceUrl() == null) {
            model.setFaceUrl(Base64Util.getImageBase("https://www.mashangjiuye.cn/file/img/0k89lsrz7K1S35e051735129320874bf64467c22e659.jpg"));
        } else {
            model.setFaceUrl(Base64Util.getImageBase(user.getFaceUrl()));
        }
        JobAdditionalModel jobAdditionalModel = jobAdditionalService.getModelByCid(cus.getId());
        if (jobAdditionalModel != null) {
            model.setAdditional(jobAdditionalModel.getData());
        } else {
            model.setAdditional("暂无信息");
        }
        if(workExpList!=null)
        {
            for (int i = 0; i < workExpList.size(); i++) {

                workExpList.get(i).setIndustryText(IndustryTool.myMap.get(workExpList.get(i).getIndustry()));
            }
        }
        model.setWorkExpList(workExpList);
        List<CusItemExpModel> itemExpList = cusItemExpService.getItemExpByCustomerId(cus.getId());//工作经历
        model.setItemExpList(itemExpList);
        List<CusEducationModel> educationList = cusEducationService.getWorkExpList(cus.getId());//教育经历
        if(educationList!=null)
        {
            for (int i = 0; i < educationList.size(); i++) {
                for (int j = 0; j < EducationTool.EDUCATION_VO_LIST.size(); j++) {
                    if(EducationTool.EDUCATION_VO_LIST.get(j).getId()==educationList.get(i).getEducation())
                    {
                        educationList.get(i).setEducationText(EducationTool.EDUCATION_VO_LIST.get(j).getData());
                    }
                }
            }
        }

        model.setEducationList(educationList);
        List<CusResumeLangModel> lang = cusResumeLangskillService.findByCustomerId(cus.getId());
        if(lang!=null)
        {
            for (int i = 0; i < lang.size(); i++) {
                for (int j = 0; j < SkilledTool.SKILL_VO_LIST.size(); j++) {
                    if(SkilledTool.SKILL_VO_LIST.get(j).getId()==lang.get(i).getRedAndWrt())
                    {
                        lang.get(i).setRedAndWrtText(SkilledTool.SKILL_VO_LIST.get(j).getData());
                        break;
                    }
                }
                LanguageModel languageModel = languageService.getModelById(lang.get(i).getLanguages());
                lang.get(i).setLanguagesText(languageModel.getLanguages());
            }
        }

        model.setLangList(lang);

        //生成pdf，指定目标文件路径
        model.parse2Pdf(SystemConstant.DEFAULT_PDF_URL+"pdf/" + user.getMobile() + ".pdf");
        String url = SystemConstant.DEFAULT_UPLOAD_SERVICE_URL +"pdf/" +  user.getMobile() + ".pdf";
        return url;
    }


}
